package util;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContextEvent;

public class TimerActualizarCapCiclo  {

    private static TimerActualizarCapCiclo instance;

    public static synchronized TimerActualizarCapCiclo getInstance(){
        if(instance == null){
            instance = new TimerActualizarCapCiclo();
        }
        return instance;
    }

    private TimerActualizarCapCiclo(){
        MyTimer t = new MyTimer();
        Timer timer = new Timer();
        Calendar date = Calendar.getInstance();
        date.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        date.set(Calendar.HOUR, 4);
        date.set(Calendar.MINUTE, 0);
        date.set(Calendar.SECOND, 0);
        date.set(Calendar.MILLISECOND, 0);
        date.set(Calendar.AM_PM, Calendar.AM);

        timer.scheduleAtFixedRate(t, date.getTime(), 1000 * 60 * 60 * 24 * 7);
    }

    class MyTimer extends TimerTask{

      @Override
        public void run() {
            try {
                    ControlDeCiclosNewlong control = new ControlDeCiclosNewlong();
                
                control.cerrarCiclos();

                control.recalcularAcarreoDeCiclos();
            }catch (SQLException ex) {
                LoggerHelper.loguearMensaje(this.getClass(), "thread - error en la actualizacion de CN",ex.getMessage() );
            }
        }
    }

    public void contextInitialized(ServletContextEvent sce) {
    }

}
